<?php
	function edit_user_form()
	{
		$user = frontController::singleton()->getCurrentUser();
		
		$form = array();
		
		$form['#action'] = './bdbdec.php?action=editMyAccount';
		
		$form['#redirect'] = FALSE;
		
		$form['form'] = array(
			'#type' => 'hidden',
			'#value' => 'data'
		);
		
		$form['data'] = array(
			'#type' => 'fieldset',
			'#title' => t('Donn&eacute;es principales'),
			'#collapsible' => TRUE
		);
		
		$form['data']['login'] = array(
			'#type' => 'textfield',
			'#title' => t('Login'),
			'#default_value' => $user->getLogin(),
			'#description' => t('Identifiant &agrave; utiliser lorsque vous vous connectez au site'),
			'#maxlength' => 30,
			'#size' => 45,
			'#required' => TRUE
		);
		
		$form['data']['lastname'] = array(
			'#type' => 'textfield',
			'#title' => t('Nom'),
			'#default_value' => $user->getLastname(),
			'#maxlength' => 20,
			'#size' => 30,
			'#required' => TRUE
		);
		
		$form['data']['firstname'] = array(
			'#type' => 'textfield',
			'#title' => t('Pr&eacute;nom'),
			'#default_value' => $user->getFirstname(),
			'#maxlength' => 20,
			'#size' => 30,
			'#required' => TRUE
		);
		
		$form['data']['email'] = array(
			'#type' => 'textfield',
			'#title' => t('Email'),
			'#default_value' => $user->getEmail(),
			'#maxlength' => 30,
			'#size' => 45,
			'#required' => TRUE
		);
		
		$form['data']['semester'] = array(
			'#type' => 'select',
			'#title' => t('Semestre'),
			'#default_value' => $user->getSemester(),
			'#options' => array(
				'1' => t('TC01'),
				'2' => t('TC02'),
				'3' => t('TC03'),
				'4' => t('TC04'),
				'5' => t('TC05'),
				'5' => t('TC06'),
				'11' => t('GX01'),
				'12' => t('GX02'),
				'13' => t('GX03'),
				'14' => t('GX04'),
				'15' => t('GX05'),
				'16' => t('GX06'),
				'17' => t('GX07'),
				'18' => t('GX08'),
				'18' => t('GX09'),
				'20' => t('Non UTC')
			)
		);
		
		$form['data']['saveRecords'] = array(
			'#type' => 'checkbox',
			'#title' => t('Autoriser la BDBDEC &agrave; publier mes donn&eacute;es')
		);
		
		if ($user->getSaveRecords())
		{
			$form['data']['saveRecords']['#attributes'] = array('checked' => 'checked');
		}
		
		$form['data']['submit'] = array(
			'#type' => 'submit',
			'#value' => t('Enregistrer')
		);
		
		return $form;
	}
	/*
	function change_user_password_form()
	{
		$form = array();
		
		$form['#action'] = './bdbdec.php?action=editMyAccount';
		
		$form['#redirect'] = FALSE;
		
		$form['form'] = array(
			'#type' => 'hidden',
			'#value' => 'passBDBDEC'
		);
		
		$form['passBDBDEC'] = array(
			'#type' => 'fieldset',
			'#title' => t('Changer de mot de passe'),
			'#collapsible' => TRUE
		);
	
		$form['passBDBDEC']['passwordBDBDEC'] = array(
			'#type' => 'password_confirm',
			'#size' => 45,
			//'#required' => TRUE
		);
	
		$form['passBDBDEC']['valid'] = array(
			'#type' => 'submit',
			'#value' => t('Enregistrer')
		);
		
		return $form;
	}
	*/
	
	$user = $controller->getCurrentUser();
	
	if ($request->getMethod() == 'POST')
	{
		if ($request->getParam('form') == 'data')
		{
			try 
			{
				$login = addslashes($request->getParam('login'));
				$lastname = addslashes($request->getParam('lastname'));
				$firstname = addslashes($request->getParam('firstname'));
				$email = $request->getParam('email');
				$semester = intval($request->getParam('semester'));
				$saveRecords = $request->getParam('saveRecords') ? true : false;
				
				$user->setLogin($login);
				$user->setLastname($lastname);
				$user->setFirstname($firstname);
				$user->setEmail($email);
				$user->setSemester($semester);
				$user->setSaveRecords($saveRecords);
				$user->update();
				
				$controller->addInformation('Modifications effectu&eacute;es.');
			}
			catch (Exception $e)
			{
				$e->_log();
				$controller->addError('Erreur : Les modifications n\'ont pas pu &ecirc;tre effectu&eacute;es.');
			}
		}
		else
		{
			try 
			{
				$password = $request->getParam('passwordBDBDEC');
				
				if ($password['pass1'] == $password['pass2'])
				{	
					if (strlen($password['pass1']) > 32) // le maxlenght bug sur password_confirm
					{
						$controller->addError('Votre mot de passe ne doit pas faire plus de 32 caract&eacute;res !');	
					}
					else
					{
						$user->changePassword($password['pass1']);
						$controller->addInformation('Mot de passe modifi&eacute;.');
					}
				}
				else
				{
					$controller->addError('Erreur : les deux mots de passes ne sont pas identiques.');
				}
				
			}
			catch (Exception $e)
			{
				$e->_log();
				$controller->addError('Erreur : Le mot de passe n\'a pas pu &ecirc;tre modifi&eacute;.');
			}
		}
	}
	
	//$controller->setVar('formIds', array('edit_user_form', 'change_user_password_form'));
	$controller->setVar('formId', 'edit_user_form');
	$controller->render('editMyAccount');
?>